package org.zhaozhen.system.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.DispatcherServlet;
import org.zhaozhen.basic.util.AjaxResult;
import org.zhaozhen.basic.util.PageList;
import org.zhaozhen.system.domain.DictionaryDetail;
import org.zhaozhen.system.query.DictionaryDetailQuery;
import org.zhaozhen.system.service.IDictionaryDetailService;

import java.util.List;

@RestController
@RequestMapping("/dicd")
@Api(tags = "数据字典接口",description = "数据字典接口详细描述")
public class DictionaryDetailController {
    @Autowired
    private IDictionaryDetailService dictionarydetailservice;

    /**
     * 新增或者修改数据字典信息
     * @param dictionarydetail
     * @return
     */
    @PutMapping
    @ApiOperation(value = "数据字典添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody DictionaryDetail dictionarydetail){
        try {
            if (dictionarydetail.getId()==null){
                dictionarydetailservice.add(dictionarydetail);
            }else {
                dictionarydetailservice.update(dictionarydetail);
            }
            return AjaxResult.me();
        }catch(Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

    /**
     * 根据ID删除一条数据
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable("id") Long id){
        System.out.println(id);
        try {
            dictionarydetailservice.remove(id);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            dictionarydetailservice.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，稍后重试！！！！");
        }
    }

    /**
     * 根据ID查询一条数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public DictionaryDetail queryById(@PathVariable("id") Long id){
        return dictionarydetailservice.queryById(id);
    }

    /**
     * 查询所有数据
     * @return
     */
    @GetMapping
    public List<DictionaryDetail> queryAll(){
        return dictionarydetailservice.queryAll();
    }
    @PostMapping("/list")
    public PageList<DictionaryDetail> queryPage(@RequestBody DictionaryDetailQuery query){
        return dictionarydetailservice.queryPage(query);
    }
    @GetMapping("/list2/{id}")
    public PageList<DictionaryDetail> queryPage(@PathVariable("id") Long id){
        System.out.println(id+"------------");
        return dictionarydetailservice.queryByTypeId(id);
    }

}
